package com.zian.controller.admin;


import com.zian.annotation.OperLog;
import com.zian.common.R;
import com.zian.domain.dto.LoginDTO;
import com.zian.domain.dto.PageDTO;
import com.zian.domain.dto.UpdateAdminDTO;
import com.zian.domain.dto.UpdatePwdDTO;
import com.zian.domain.po.SysAdminUser;
import com.zian.domain.query.UserQuery;
import com.zian.domain.vo.UserQueryVO;
import com.zian.enums.BusinessType;
import com.zian.service.ISysAdminUserService;
import com.zian.validation.ValidGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-09-08
 */

@Slf4j
@Api("后台管理员接口")
@RestController
@RequestMapping("/admin/admins")
@RequiredArgsConstructor
public class SysAdminUserController {

    private final ISysAdminUserService adminUserService;

    // 删除
    @DeleteMapping
    @ApiOperation("删除员工")
    @OperLog(title = "管理员模块", businessType = BusinessType.DELETE)
    public R deleteAdminById(@RequestParam("ids") List<Long> ids) {
        log.info("删除管理员数据：{}", ids);
        adminUserService.deleteAdminById(ids);
        return R.ok();
    }

    @PutMapping("/{id}")
    @ApiOperation("禁用员工")
    @OperLog(title = "管理员模块", businessType = BusinessType.UPDATE)
    public R updateLocked(@PathVariable Long id) {
        log.info("禁用管理员数据：{} .", id);
        adminUserService.updateLocked(id);
        return R.ok();
    }


    @PostMapping
    @ApiOperation("新增员工")
    @OperLog(title = "管理员模块", businessType = BusinessType.INSERT)
    public R addAdmin(@RequestBody @Validated(ValidGroup.Crud.Create.class) UpdateAdminDTO dto) {
        log.info("新增管理员数据：{}", dto);
        adminUserService.addAdmin(dto);
        return R.ok();
    }

    // 登录接口
    @PostMapping("/login")
    @ApiOperation("管理员登录接口")
    public R<String> login(@RequestBody LoginDTO dto) {
        log.info("管理员登录接口 , 参数：{}", dto);
        return R.ok(adminUserService.login(dto));
    }

    // 查询管理员信息
    @GetMapping("/user-info/{id}")
    @ApiOperation("查询用户信息")
    public R<SysAdminUser> getUserInfo(@PathVariable Long id) {
        return R.ok(adminUserService.getUserInfo(id));
    }

    @GetMapping("/page")
    @ApiOperation("管理员列表")
    public R<PageDTO<UserQueryVO>> queryPage(UserQuery query) {
        log.info("管理员列表接口 , 参数：{}", query);
        PageDTO<UserQueryVO> page = adminUserService.queryPage(query);
        return R.ok(page);
    }


    @PostMapping("/logout")
    @ApiOperation("注销")
    public R logout(@RequestHeader(value = "Authorization", required = false) String token) {
        adminUserService.logout(token);
        return R.ok();
    }

    // 修改个人信息
    @PutMapping("/update-info")
    @ApiOperation("修改个人信息")
    @OperLog(title = "管理员模块", businessType = BusinessType.UPDATE)
    public R updateInfo(@RequestBody @Validated(ValidGroup.Crud.Update.class) UpdateAdminDTO dto) {
        log.info("修改个人信息接口 , 参数：{}", dto);
        adminUserService.updateInfo(dto);
        return R.ok();
    }


    // 修改密码
    @PutMapping("/update-pwd")
    @ApiOperation("修改密码")
    @OperLog(title = "管理员模块", businessType = BusinessType.UPDATE)
    public R updatePwd(@RequestBody @Validated UpdatePwdDTO dto) {
        log.info("修改密码接口 , 参数：{}", dto);
        adminUserService.updatePwd(dto);
        return R.ok();
    }


}
